#import <Foundation/Foundation.h>
#import <objc/runtime.h>

#import "FWCommon.h"
#import "FWDetailListView.h"
#import "FWDetailListEntry.h"


@implementation FWDetailListEntry

@synthesize icon, link, size, color, lines, title, value, info, button, message, notBold, backLink, progress, selected, switcher, draggable, backgroundColor, key, accessoryView, colorAccessory;



- (id)copyWithZone:(NSZone*)zone
{
    FWDetailListEntry* copy = [[[self class] allocWithZone:zone] init];
    
    copy.title = title;
    copy.value = value;
    copy.message = message;
    copy.key = key;
    copy.info = info;
    copy.notBold = notBold;
    copy.size = size;
    copy.icon = icon;
    copy.lines = lines;
    copy.color = color;
    copy.backgroundColor = backgroundColor;
    copy.accessoryView = accessoryView;
    copy.colorAccessory = colorAccessory;
    copy.progress = progress;
    copy.switcher = switcher;
    copy.selected = selected;
    copy.link = link;
    copy.backLink = backLink;
    copy.button = button;
    copy.draggable = draggable;

    return copy;
}

+ (FWDetailListEntry*)entryWithTitle:(NSString*)newTitle
{
    FWDetailListEntry* entry = [[[FWDetailListEntry alloc] init] autorelease];
    
    entry.title = newTitle;
    
    return entry;
}

+ (FWDetailListEntry*)entryWithTitle:(NSString*)newTitle value:(NSObject*)newValue
{
    FWDetailListEntry* entry = [[[FWDetailListEntry alloc] init] autorelease];
    
    entry.title = newTitle;
    entry.value = newValue;
    
    return entry;
}

+ (FWDetailListEntry*)entryWithLocalisedTitle:(NSString*)newTitle bundle:(NSBundle*)bundle
{
    FWDetailListEntry* entry = [[[FWDetailListEntry alloc] init] autorelease];
    
    entry.title = localize(bundle, newTitle);
    
    return entry;
}

+ (FWDetailListEntry*)entryWithLocalisedTitle:(NSString*)newTitle value:(NSObject*)newValue bundle:(NSBundle*)bundle
{
    FWDetailListEntry* entry = [[[FWDetailListEntry alloc] init] autorelease];
    
    entry.title = localize(bundle, newTitle);
    entry.value = newValue;
    
    return entry;
}

+ (FWDetailListEntry*)entryWithLocalisedTitle:(NSString*)newTitle localisedValue:(NSString*)newValue bundle:(NSBundle*)bundle
{
    FWDetailListEntry* entry = [[[FWDetailListEntry alloc] init] autorelease];
    
    entry.title = localize(bundle, newTitle);
    entry.value = localize(bundle, newValue);
    
    return entry;
}

- (id)init
{
    self = [super init];
    if (self) {
        size = kFWDetailListViewLabelSize;
        lines = -1;

        self.color = [UIColor darkTextColor];
        self.backgroundColor = [UIColor whiteColor];
    }
    
    return self;
}

- (void)dealloc
{
    [title release];
    [key release];
    [message release];
    [value release];
    [info release];
    [icon release];
    [color release];
    [accessoryView release];
    [backgroundColor release];
    
    [super dealloc];
}


+ (CGFloat)fontSizeForTitle
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        return kFWDetailListViewLabelSize + 3.0;
    else
        return kFWDetailListViewLabelSize * 2;
}


- (FWDetailListEntry*)withProgressType
{
    progress = YES;
    return self;
}

- (FWDetailListEntry*)withLinkType:(id)newKey
{
    self.key = newKey;
    link = YES;
    return self;
}

- (FWDetailListEntry*)withBackLinkType:(id)newKey
{
    self.key = newKey;
    backLink = YES;
    return self;
}

- (FWDetailListEntry*)withBackgroundColor:(UIColor*)newColor
{
    self.backgroundColor = newColor;
    return self;
}

- (FWDetailListEntry*)withNoBold
{
    self.notBold = YES;
    return self;
}

- (FWDetailListEntry*)withColor:(UIColor*)newColor
{
    self.color = newColor;
    return self;
}

- (FWDetailListEntry*)withSize:(CGFloat)newSize
{
    size = newSize;
    return self;
}

- (FWDetailListEntry*)withLines:(NSInteger)newLines
{
    lines = newLines;
    return self;
}

- (FWDetailListEntry*)withValue:(NSObject*)newValue
{
    self.value = newValue;
    return self;
}

- (FWDetailListEntry*)withIcon:(UIImage*)newIcon
{
    self.icon = newIcon;
    return self;
}

- (FWDetailListEntry*)withIconFile:(NSString*)newIcon
{
    self.icon = [UIImage imageWithContentsOfFile:newIcon];
    return self;
}

- (FWDetailListEntry*)withKey:(id)newKey
{
    self.key = newKey;
    return self;
}

- (FWDetailListEntry*)withDraggableType:(id)newKey
{
    self.key = newKey;
    self.draggable = YES;
    return self;
}

- (FWDetailListEntry*)withButtonType:(id)newKey
{
    self.key = newKey;
    self.button = YES;
    return self;
}

- (FWDetailListEntry*)withAccessoryView:(UIView*)newView
{
    self.accessoryView = newView;
    return self;
}

- (FWDetailListEntry*)withColorAccessoryView:(UIView*)newView;
{
    self.accessoryView = newView;
    colorAccessory = YES;
    return self;
}


- (BOOL)isControl
{
    return progress || switcher || button || selected || link || backLink;
}

- (NSString*)description
{
        return [NSString stringWithFormat:
                @"<%@: %p;"
                " title = %@;"
                " message = %@;"
                " key = %@;"
                " value = %@;"
                " info = %@;"
                " notBold = %i;"
                " size = %f;"
                " icon = %@;"
                " lines = %i;"
                " color = %@;"
                " backgroundColor = %@;"
                " progress = %i;"
                " switcher = %i;"
                " selected = %i;"
                " link = %i;"
                " backLink = %i;"
                " button = %i;"
                " draggable = %i>",
                [self class],
                self,
                self.title,
                self.message,
                self.key,
                self.value,
                self.info,
                self.notBold,
                self.size,
                self.icon,
                self.lines,
                self.color,
                self.backgroundColor,
                self.progress,
                self.switcher,
                self.selected,
                self.link,
                self.backLink,
                self.button,
                self.draggable];
}

@end
